import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:get/get_connect/connect.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jxchmz/API/LoginApi.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';
import 'package:shared_preferences/shared_preferences.dart';

class UserMessage {
  LoginApi loginApi = new LoginApi();
  Dio dio = new Dio();
  var ResponseData;

  // 获取本地token
   configDio() async {
    // 获取本地token
    GetStorage storage = GetStorage();
    String Token =await storage.read("Token");
    print("token:$Token");
    print("账号:${await storage.read("Phone")}");
    print("密码:${await storage.read("Password")}");
    // 构造请求头
    dio.options.contentType="application/json";
    dio.options.headers['Authorization'] = "Bearer $Token";
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
  }

//  获取用户信息
  GetUser() async {
    //配置dio
   await configDio();
   try{
     ResponseData = await dio
       .get(loginApi.ApiUrl + "/getInfo");
   if (ResponseData.statusCode == 200) {
     // 请求成功
     ResponseData =
         json.decode(ResponseData.toString());
     log("数据返回:$ResponseData");
     return ResponseData;
   } else {
     log("请求失败，状态码:${ResponseData.statusCode}");
     GetxTools().ErrorToast("系统异常");
     return "";
   }
   }catch(e){
     GetxTools().ErrorToast("系统异常");
     return "";
   }

  }
}
